import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Set;

public class FirstTest {
    ChromeDriver driver = null;
    void createDriver(){
        //1.用驱动管理来打开ChromeDriver，也就是创建谷歌驱动
        WebDriverManager.chromedriver().setup();
        //增加浏览器配置：创建驱动对象要强制指定允许访问所有的链接
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        //创建一个驱动对象，通过driver对象发送指令
        driver = new ChromeDriver(options);
        //输入百度网址
        driver.get("https://www.baidu.com");
    }
    void test1(){
        createDriver();
        //找到输入框并输入”迪丽热巴“
        driver.findElement(By.cssSelector("#kw")).sendKeys("迪丽热巴");
        //找到百度一下按钮并点击
        driver.findElement(By.cssSelector("#su")).click();
        //关闭浏览器
        driver.close();
    }
    void test2(){
        createDriver();
        WebElement element= driver.findElement(By.cssSelector("#hotsearch-content-wrapper > li:nth-child(1) > a > span.title-content-title"));
        List<WebElement> list = driver.findElements(By.cssSelector("#hotsearch-content-wrapper > li>a>span.title-content-title"));
        for (WebElement element1:list){
            System.out.println(element1.getText());
        }
        driver.quit();
    }
    void test3() throws InterruptedException {
        createDriver();
        Thread.sleep(3000);
        driver.manage().window().maximize();

        driver.manage().window().minimize();
        driver.manage().window().setSize(new Dimension(1024,654));
    }


    void test4() throws IOException {
        //低阶屏幕截图
        createDriver();
        //切换屏幕
        String handle = driver.getWindowHandle();
        driver.findElement(By.cssSelector("#s-hotsearch-wrapper > div > a.hot-title > div > i:nth-child(1)")).click();
        Set<String> windowHandles = driver.getWindowHandles();
        String destHandle = "";
        for (String handle1:windowHandles) {
             destHandle = handle1;
        }
        driver.switchTo().window(destHandle);
        driver.findElement(By.cssSelector("#sanRoot > header > div.container.tab-wrap_23sEv > div.c-theme-color.tabs-wrap_3Ac9n > a:nth-child(2) > span")).click();
//        //截图文件
//        File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//        //将文件另存
//        FileUtils.copyFile(srcFile, new File("image.png"));
        //实现高阶的屏幕截图
        getScreenshot(getClass().getName());
        driver.quit();
    }
    //封装屏幕截图方法
    void getScreenshot(String str) throws IOException {
        //先将保存截图的文件以及文件名称创建好
        //文件夹名
        SimpleDateFormat sim1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sim2 = new SimpleDateFormat("HHmmss");
        //获得年月日时间
        String dirTime = sim1.format(System.currentTimeMillis());
        String fileTime = sim2.format(System.currentTimeMillis());
        //构造文件名称
        String filename = "./src/test/image/"+dirTime+"/"+str+"-"+fileTime+".png";
        System.out.println(filename);
        //执行截图操作
        File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        //将文件保存到指定位置
        FileUtils.copyFile(srcFile, new File(filename));
    }
}
